//
//  UIPopupController.m
//  storeapp
//
//  Created by huangyi on 13-4-11.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "UIPickerController.h"
#import "UIView+utils.h"

@interface UIPickerController () <UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *arrPicker;
@property(nonatomic,strong)NSString *txtTitle;

@end

@implementation UIPickerController

//+(UIPickerController*)shared{
//    static UIPickerController *singleton=nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        singleton=[[UIPickerController alloc] init];
//    });
//    return singleton;
//}

//-(NSMutableString*)presentPickerArray:(NSArray*)data onView:(UIView*)view{
//    self.arrPicker=data;
//    NSMutableString *string=[NSMutableString string];
//    UIViewController *controller=[[UIViewController alloc] init];
//    UIPickerView *picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 300, 520)];
//    controller.view=picker;
//    picker.dataSource=self;
//    picker.delegate=self;
//    UIPopoverController *popover=[[UIPopoverController alloc] initWithContentViewController:controller];
//    [popover presentPopoverFromRect:view.frame inView:view.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
//    self.popover=popover;
//    return string;
//}

-(UIPickerController*)initWithTitle:(NSString*)title andChoice:(NSArray *)array{
    self = [super init];
    if (self) {
        self.arrPicker=array;
        self.txtTitle=title;
    }
    return self;
}

-(void)showPicker{
    UITableViewController *table=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    table.title=self.txtTitle;
    table.tableView.dataSource=self;
    table.tableView.delegate=self;
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:table];
    nav.modalPresentationStyle=UIModalPresentationFormSheet;
    [self.delegate presentViewController:nav animated:YES completion:nil];
    table.tableView.userInfo=self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrPicker.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"picker"];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"picker"];
        cell.clipsToBounds=YES;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    NSArray *array=self.arrPicker;
    id object=array[indexPath.row];
    if ([object isKindOfClass:[NSString class]]) {
        cell.textLabel.text=object;
    }else{
        cell.userInfo=object;
        cell.textLabel.text=object[@"description"];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(pickerController:didSelectedItemAtIndex:withInfo:)]) {
        [self.delegate pickerController:self didSelectedItemAtIndex:indexPath.row withInfo:self.arrPicker[indexPath.row]];
    }
    [self.delegate dismissViewControllerAnimated:YES completion:nil];
    tableView.userInfo=nil;    
}

-(NSString*)title{
    return self.txtTitle;
}

@end
